// store/modules/timer.js
export default {
    namespaced: true,
    state: () => ({
        timer: null,
        timeElapsed: {}
    }),
    mutations: {
        START_TIMER(state) {
            state.timer = setInterval(() => {
                const now = Date.now();
                const elapsed = {};
                state.orders.processing.forEach(order => {
                    if (order.startTime) {
                        const diffMs = Math.max(0, now - order.startTime);
                        const minutes = Math.floor(diffMs / 60000);
                        const seconds = Math.floor((diffMs % 60000) / 1000);
                        elapsed[order.orderId] = `${minutes}分${seconds}秒`;
                    }
                });
                state.timeElapsed = elapsed;
            }, 1000);
        },
        STOP_TIMER(state) {
            if (state.timer) clearInterval(state.timer);
            state.timer = null;
        }
    },
    actions: {
        startTimer({ commit }) {
            commit('START_TIMER');
        },
        stopTimer({ commit }) {
            commit('STOP_TIMER');
        }
    }
};
